import axios from '@/utils/request'
const store = localStorage.getItem('storeId')
export default {
  StoreAttendanceLog: {
    entity: { name: 'AttendanceLog', prefix: () => `store/${localStorage.getItem('storeId')}` },
    list: {
      disabled_actions: ['new', 'lines'],
      query: {
        '@order': 'entity.id|DESC',
        '@expands': '["entity.staff"]'
      },
      list_filter: {
        beforeCreatedTime: {
          expression: 'entity.getCreatedTime() >= datetime.get(":value 00:00:00")',
          label: '创建开始时间',
          type: 'date'
        },
        'staff.squad.id': () => {
          return axios
            .get(`/store/${store}/squads`)
            .then(res =>
              Object.assign({ __label: '小队' },
                ...res.data.map(v => { return { [v.id]: v.name } })))
        },
        'staff.role.id': () => {
          return axios
            .get(`/store/${store}/roles`)
            .then(res =>
              Object.assign({ __label: '角色' },
                ...res.data.map(v => { return { [v.id]: v.name } })))
        },
        afterCreatedTime: {
          expression: 'entity.getCreatedTime() <= datetime.get(":value 23:59:59")',
          label: '创建结束时间',
          type: 'date'
        },
        date: {
          expression: 'entity.getDate() >= datetime.get(":value 00:00:00") && entity.getDate() <= datetime.get(":value 23:59:59")',
          label: '营业日期',
          type: 'date'
        }
      },
      list_display: [
        'id',
        'staff',
        { property: 'staff.__metadata.squad.__toString', label: '小队' },
        { property: 'staff.__metadata.role.__toString', label: '角色' },
        { property: 'type', label: '班次' },
        {
          property: 'phase',
          label: '上落班',
          component: {
            props: ['data'],
            render(h) {
              if (this.data) {
                return <span>上班</span>
              } else {
                return <span>落班</span>
              }
            }
          }
        },
        {
          property: 'date',
          label: '营业日期'
        },
        'createdTime'
      ]
    },
    form: {
      fields: [
        'id',
        {
          property: 'user',
          field_options: { label: 'user' },
          component: {
            methods: {
              setInput(value) {
                if (value) {
                  this.form[this.property] = value
                } else {
                  this.form[this.property] = null
                }
              }
            },
            watch: {
              'form.staff': {
                handler(value, oldValue) {
                  this.setInput(value)
                },
                immediate: true
              }
            },
            props: ['form', 'property'],
            render(h) {
              return (
                <el-input v-model={this.form[this.property]} disabled={true}></el-input>
              )
            }
          }
        },
        'staff',
        'type',
        'phase',
        'createdTime'
      ]
    }
  }
}

